<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/login.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container ref="homePage">
      <!-- 侧边栏 -->
      <!-- 点击折叠栏时宽度变化 -->
      <el-aside :width="isCollapse ? '64px' : '180px'">
        <div class="toggle-button el-icon-view" @click="toggleCollapse"></div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#F8F9FB"
          text-color="black"
          active-text-color="#d43c33"
          :collapse="isCollapse"
          unique-opened
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-menu-item
            :index="'/' + item.path"
            v-for="item in menulist"
            :key="item.id"
            @click="saveNavState('/' + item.path)"
          >
            <!-- 一级菜单的模板区域 -->
            <!-- 动态顺序添加图标，id作为对象属性值 -->
            <i :class="iconObj[item.id]"></i>
            <!-- 文本 -->
            <span>{{ item.authName }}</span>
            <!-- 二级菜单 -->
            <!-- <el-menu-item
              v-for="item1 in item.children"
              :index="'/' + item1.path"
              :key="item1.id"
              @click="saveNavState('/' + item1.path)"
            >
              <template slot="title"> -->
            <!-- 图标 -->
            <!-- <i class="el-icon-menu"></i> -->
            <!-- 文本 -->
            <!-- <span>{{ item1.authName }}</span>
              </template>
            </el-menu-item> -->
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        "101": "el-icon-s-custom",
        "102": "el-icon-s-order",
        "103": "el-icon-s-claim",
        "104": "el-icon-s-data",
        "105": "el-icon-document",
        "106": "el-icon-folder-checked"
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: "",
      // 高度
      clientHeight: ""
    };
  },
  //刚创建时
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  mounted() {
    // 获取浏览器可视区域高度
    this.clientHeight = `${document.documentElement.clientHeight}`;
    window.onresize = function temp() {
      this.clientHeight = `${document.documentElement.clientHeight}`;
    };
  },
  watch: {
    // 监视高度变化
    clientHeight: function() {
      this.changeFixed(this.clientHeight);
    }
  },
  methods: {
    //动态修改样式
    changeFixed(clientHeight) {
      this.$refs.homePage.$el.style.height = clientHeight - 20 + "px";
    },
    async logout() {
      const { data: res } = await this.$http.get("user/logout");
      if (res.code !== 200) return this.$message.error("退出失败！,请稍后重试");
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有的菜单
    async getMenuList() {
      // 菜单
      this.menulist = [
        {
          id: 101,
          authName: "用户管理",
          path: "users"
        },
        {
          id: 102,
          authName: "考题管理",
          path: "questions"
        },
        {
          id: 103,
          authName: "考试管理",
          path: "test"
        },
        {
          id: 104,
          authName: "成绩管理",
          path: "score"
        },
        {
          id: 105,
          authName: "文章管理",
          path: "text"
        },
        {
          id: 106,
          authName: "考勤管理",
          path: "check"
        }
      ];
    },
    //点击按钮切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    span {
      color: #000000;
    }
  }
  img {
    height: 40px;
    width: 40px;
    padding-left: 15px;
    padding-right: 5px;
  }
}
.el-aside {
  background-color: #ffffff;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  background-color: #d43c33;
  width: 100%;
  color: #fff;
  line-height: 24px;
  text-align: center;
  cursor: pointer;
}
</style>